use crate::file;
use fast_log::consts::LogSize;
use fast_log::plugin::file_split::RollingType;
use fast_log::plugin::packer::LogPacker;
use lazy_static::lazy_static;
use std::sync::RwLock; // 1.4.0
lazy_static! {
  pub static ref SQL_LOG_PATH: RwLock<String> = RwLock::new(String::from("."));
  pub static ref DATABASE_URL: RwLock<String> = RwLock::new(String::from("."));
  pub static ref KEY_PATH: RwLock<String> = RwLock::new(String::from("."));
  pub static ref CERT_PATH: RwLock<String> = RwLock::new(String::from("."));
}

pub fn init(home_dir: &str, resource_dir: &str, database_url_:&str, sqllog_path_:&str) {



  let sqllog_path = format!("{}/{}", home_dir,database_url_);
  let database_url = format!("{}/{}", home_dir,sqllog_path_);

  println!("common init got sqllog_path={}",sqllog_path);
  println!("common init got database_url={}",database_url);

  // enable log crate to show sql logs
  let result = fast_log::init_split_log(
    &sqllog_path,
    LogSize::MB(1),
    RollingType::All,
    log::Level::Info,
    None,
    Box::new(LogPacker {}),
    true,
  );
  if result.is_ok() {
    println!(
      "created {} to enable log crate to show sql logs",
      &sqllog_path
    );
  }

  // create sqlite database file
  let _file = file::cropen_file_path(&database_url);

  let mut n1 = SQL_LOG_PATH.write().unwrap();
  *n1 = sqllog_path;

  let mut n2 = DATABASE_URL.write().unwrap();
  *n2 = database_url;

  let mut n3 = KEY_PATH.write().unwrap();
  *n3 = format!("{}/resources/key.pem", resource_dir);

  let mut n4 = CERT_PATH.write().unwrap();
  *n4 = format!("{}/resources/cert.pem", resource_dir);

  // let aa = KEY_PATH.read().unwrap();
  // let ss:String = aa.to_owned();
}
